<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">车间</label>
        <el-input v-model="query.facility" clearable placeholder="车间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">车间代码</label>
        <el-input v-model="query.facilitycode" clearable placeholder="车间代码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">车间名称</label>
        <el-input v-model="query.facilityname" clearable placeholder="车间名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">部门</label>
        <treeselect
          class="filter-item"
          v-model="query.deptid"
          :options="depts"
          :load-options="loadDepts"
          style="width: 185px"
          placeholder="部门"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="车间" prop="facility">
            <el-input v-model="form.facility" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="车间代码" prop="facilitycode">
            <el-input v-model="form.facilitycode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="车间名称" prop="facilityname">
            <el-input v-model="form.facilityname" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="部门" prop="dept.id">
            <treeselect
              v-model="form.dept.id"
              :options="depts"
              :load-options="loadDepts"
              style="width: 370px"
              placeholder="选择部门"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="facility" label="车间" />
        <el-table-column prop="facilitycode" label="车间代码" />
        <el-table-column prop="facilityname" label="车间名称" />
        <el-table-column prop="dept" :show-overflow-tooltip="true" label="部门">
          <template slot-scope="scope">
            <div>{{ scope.row.dept.name }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','facility:edit','facility:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-dle="scope.row.id === facility.id"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudFacility from '@/api/system/facility'
import { getDepts, getDeptSuperior } from '@/api/system/dept'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import facility from '@/api/system/facility'

const defaultForm = { facility: null, facilitycode: null, facilityname: null, dept: { id: null }}
export default {
  name: 'Facility',
  computed: {
    facility() {
      return facility
    }
  },
  components: { Treeselect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '系统：车间', url: 'api/facility', idField: 'facility', sort: 'facility,desc', crudMethod: { ...crudFacility }})
  },
  data() {
    return {
      depts: [], deptDates: [],
      permission: {
        add: ['admin', 'facility:add'],
        edit: ['admin', 'facility:edit'],
        del: ['admin', 'facility:del']
      },
      rules: {
        facility: [
          { required: true, message: '车间不能为空', trigger: 'blur' }
        ],
        facilitycode: [
          { required: true, message: '车间代码不能为空', trigger: 'blur' }
        ],
        facilityname: [
          { required: true, message: '车间名称不能为空', trigger: 'blur' }
        ],
        dept: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ]
      }
      // queryTypeOptions: [
      //   { key: 'facility', display_name: '车间' },
      //   { key: 'facilitycode', display_name: '车间代码' },
      //   { key: 'facilityname', display_name: '车间名称' },
      //   { key: 'deptno', display_name: '部门' }
      // ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.getDepts()
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        this.getDepts()
      } else {
        this.getSupDepts(form.dept.id)
      }
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    buildDepts(depts) {
      depts.forEach(data => {
        if (data.children) {
          this.buildDepts(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null
        }
      })
    },
    getSupDepts(deptId) {
      getDeptSuperior(deptId).then(res => {
        const date = res.content
        this.buildDepts(date)
        this.depts = date
      })
    },
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    handleChange(value) {
      this.depts.deptId = value
    }
  }
}

</script>

<style scoped>

</style>
